﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Legazpi.Payroll.BusinessLogic.Services;
using Legazpi.Payroll.BusinessLogic.Services.Interfaces;
using Ninject;
using Web.Codes;

namespace Legazpi.Payroll.Web.Admin
{
    public partial class Employees : System.Web.UI.Page
    {
        [Inject]
        public IEmployeeService _employeeService { get; set; }
        public bool IsSuccess { get; set; }
        public string NotifyText { get; set; }

        #region Implementations
        void BindGrid()
        {
            repeater.DataSource = _employeeService.Get(0, 15)
                .Select(x => new
                {
                    Id = x.Id,
                    EmployeeCode = x.EmployeeCode,
                    Fullname = string.Format("{0}, {1} {2}", x.LastName, x.FirstName, x.MiddleName),
                    Nickname = x.NickName,
                    Added = x.DateAdded.HasValue ? x.DateAdded.Value.ToString("MMM. dd,yyyy") : "n/a",
                    Position = x.Position != null ?  x.Position.Code.ToString() : string.Empty
                }).ToList();

            repeater.DataBind();
        }

        void Delete(int id)
        {
            _employeeService.Delete(id);
            IsSuccess = _employeeService.Save();
            NotifyText = "Successfully Deleted.";
            BindGrid();
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
                BindGrid();
        }

        protected void repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int intTemp = 0;
            if (int.TryParse((string)e.CommandArgument, out intTemp))
            {
                switch (e.CommandName.ToLower())
                {
                    case Constants.Delete_Command:
                        Delete(intTemp);
                        break;
                }
            }
        }
 
    }
}